<?php

/**
 * 微信菜单管理
 * 
 * Apps/Mp/Controller/MerWxMenuController.class.php
 * Zix @ 2015-07-02
 */

namespace Mp\Controller;

use Common\Service\MerWxMenuService;
//use Common\Service\MerWxSettingService;
//use Common\Service\WechatSdk;
use Common\Service\WechatSdkService;

class MerWxMenuController extends MpController {

  var $WechatSdk = null ;
  
  public function index( $wx_id ) {

    $data = $this->_init_data();
    $data['module_name'] = '微信菜单';
    $data['module_url'] = U( $this->base_path . 'index');

    $data['catalog_name'] = '商户';
    $data['catalog_url'] = U( $this->base_path . 'index') .'/';
        
    $MerWxMenu = MerWxMenuService::instance();

    $data['config'] = array(
      'menu_id' => 'menu_Mp_MerWxSetting_index',
      'mer_id' => $this->mer_id ,
      'wx_id' => $wx_id ,
      'module_url' => U($this->base_path . 'index'),
      'wx_menu'   => json_decode( $MerWxMenu->get_by_id( $wx_id ) , TRUE) ,
      'save_menu_url' => U($this->base_path . 'save_menu'),
      'get_menu_url' => U($this->base_path . 'get_menu'),
      'post_menu_url' => U($this->base_path . 'post_menu'),
    );

    $data['param'] = array();

    $this->display_with_layout($data, $this->controller .':index');
  }

  function add_main_menu() {
    $this->display('main_menu');
  }
  
  function get_menu() {
    $wx_id = I('get.wx_id');

    if ( !$this->WechatSdk ) {
      $this->WechatSdk = new WechatSdkService( $this->mer_id , $wx_id );
    }

    $ret = $this->WechatSdk->getMenu();
    if ( !$ret ) {
      $this->ajaxReturn(ajax_arr( $this->WechatSdk->errMsg ) );
    }
    
    //开始解析菜单
    $MerWxMenu = MerWxMenuService::instance();
    $data = $MerWxMenu->wx_data_2_self_data($ret);
    
    $new_ret['data'] = $data ;
    $MerWxMenu->save_menu($wx_id, $data);

    $new_ret = array(
      'status' => TRUE  ,
      'info'   => '获取菜单成功' ,
      'data' => $data
    );
    $this->ajaxReturn($new_ret);
  }
  
  function read_test() {
    $json = '{"menu":{"button":[{"type":"view","name":"11","url":"https:\/\/open.weixin.qq.com\/connect\/oauth2\/authorize?appid=wx9d23148f770ddfe1&redirect_uri=http%3A%2F%2Fgt.whcmxh.com%2Fstore%3Fmer_id%3D1%26wx_id%3D25&response_type=code&scope=snsapi_base&state=#wechat_redirect","sub_button":[]},{"name":"\u5fae\u5e2e\u5e2e","sub_button":[{"type":"click","name":"\u95ee\u9898\u89e3\u7b54","key":"\u5fae\u5b66\u5802","sub_button":[]},{"type":"click","name":"\u5c1a\u751f\u6d3b\u771f\u4ebashow","key":"\u5c1a\u751f\u6d3b","sub_button":[]},{"type":"click","name":"\u5e2e\u4f60\u6fc0\u6d3b","key":"\u6fc0\u6d3b","sub_button":[]},{"type":"view","name":"\u4e2d\u5956\u7eaa\u5f55","url":"https:\/\/open.weixin.qq.com\/connect\/oauth2\/authorize?appid=wx994b24832655b084&redirect_uri=http%3A%2F%2Fccb.sunbw.cn%2FWechat%2FWxEventUser%2Findex%2F3&response_type=code&scope=snsapi_base&state=3#wechat_redirect;","sub_button":[]}]},{"name":"\u5fae\u91d1\u878d","sub_button":[{"type":"view","name":"\u5f00\u6237\u4e00\u70b9\u901a","url":"https:\/\/open.weixin.qq.com\/connect\/oauth2\/authorize?appid=wx994b24832655b084&redirect_uri=http%3A%2F%2Fccb.sunbw.cn%2FWechat%2FCcb%2Findex%2F3&response_type=code&scope=snsapi_base&state=3#wechat_redirect;","sub_button":[]},{"type":"click","name":"\u7ed3\u7b97\u4e0e\u73b0\u91d1\u7ba1\u7406","key":"\u7ed3\u7b97\u4e0e\u73b0\u91d1\u7ba1\u7406","sub_button":[]},{"type":"click","name":"\u5fae\u7f51\u7ad9","key":"\u6e56\u5317\u5efa\u884c","sub_button":[]},{"type":"click","name":"\u5fae\u4fe1\u94f6\u884c","key":"\u5fae\u4fe1\u94f6\u884c","sub_button":[]}]}]}}';
    $ret = json_decode($json , TRUE );
    
    //print_r($ret);
    $MerWxMenu = MerWxMenuService::instance();
    $data = $MerWxMenu->wx_data_2_self_data($ret);

    print_r( $data );
  }


  /**
   * 保存菜单到本地数据库
   */
  function save_menu() {
    $data = I('post.data');
    $wx_id = I('post.wx_id');

    foreach ( $data['button'] as $key => $row ) {
      if ( !isset( $row['sub_button'] ) ) {
        $data['button'][$key]['sub_button'] = array();
      }
    }
    
    $MerWxMenu = MerWxMenuService::instance();
    $ret  = $MerWxMenu->save_menu($wx_id, $data);
    
    $this->ajaxReturn($ret);
  }
    
  /**
   * 向服务器发布菜单并保存
   * @param type $id
   */
  function post_menu($id = 0) {
    $data = I('post.data');
    $wx_id = I('post.wx_id');
    
    foreach ( $data['button'] as $key => $row ) {
      if ( !isset( $row['sub_button'] ) ) {
        $data['button'][$key]['sub_button'] = array();
      }
    }
    
    //先保存
    $MerWxMenu = MerWxMenuService::instance();
    $MerWxMenu->save_menu($wx_id, $data);
    
    //转换为微信格式
    $wx_data = $MerWxMenu->self_data_2_wx_data($data);
        
    //开始发布
    if ( !$this->WechatSdk ) {
      $this->WechatSdk = new WechatSdkService( $this->mer_id , $wx_id );
    }
    
    $ret = $this->WechatSdk->createMenu($wx_data);
   
    //发布结果
    if ( !$ret ) {
      $this->ajaxReturn(ajax_arr( $this->WechatSdk->errMsg ) );
    } else {
      $this->ajaxReturn(ajax_arr('发布菜单成功' , TRUE ) );
    }
  }

}
